<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // var obj = {
        //     id: 1,
        //     name: 'andy',
        //     msg: {
        //         age: 18
        //     }
        // }

        // var o = {};
        // Object.assign(o, obj);

        //深拷贝
        var obj = {
            id: 1,
            name: 'andy',
            msg: {
                age: 18
            },
            color: ['pink', 'blue']
        }

        var o = {};
        //封装函数
        function deepCopy(newobj, oldobj) {
            for(var k in oldobj) {
                //判断我们的属性值属于哪种类型
                //获取属性值 oldobj[k]
                var item = oldobj[k];
                //判断这个值是否是数组
                if(item instanceof Array) {
                    newobj[k] = [];
                    deepCopy(newobj[k], item);
                } else if(item instanceof Object) {
                    //判断这个值是否是对象
                    newobj[k] = {};
                    deepCopy(newobj[k], item);
                } else {
                    //属于简单数据类型
                    newobj[k] = item;
                }
            }
        }
        deepCopy(o, obj);
        console.log(o);
    </script>
</body>
</html>